$(document).ready(function () {

//搜索
    var $search = $("#section");
    $search.keyup(function () {
        var $keyword = $(this).val();
        if ($keyword !== '') {
            $.ajax({
                url: "/admincourse/section/search/",
                dataType: "json",
                type: "POST",
                data: {"keyword": $keyword},
                success: function (res) {
                    if (res.errno == 0) {
                        var searchLi = [];
                        if (res.data.length == 0) {
                            return;
                        }
                        for (var i = 0; i < res.data.length; i++) {
                            searchLi.push("<li data-section_id=" + res.data[i].section_id + " data-section_name= " + res.data[i].name + ">" + res.data[i].name + "</li>");
                        }
                        searchLi.join('');
                        $("#search").html(searchLi);
                        $("#search li").click(function () {
                            section_id = $(this).attr("data-section_id");
                            section_name = $(this).attr("data-section_name");
                            $("#section").val($(this).html());
                        });
                    } else {
                        alert(res.errmsg);
                    }
                }
            });
            $("#search").show();
        } else {
            $("#search").hide();
        }
    });
    $(".search-wrap").mouseleave(function (e) {
        $("#search").hide();
    });
    $("#search li").click(function () {
        $("#section").val($(this).html());
    });
    //添加所属的小节
    var section_id = null; //添加的小节id
    var section_name = null; //添加的小节id
    $(".addbtn").click(function () {
        if ($("#section").val() !== '') {
            var proAddHtml = "";
            if (section_id !== null) {
                proAddHtml = '<tr>'
                        + '    <td class="center">' + section_id + '</td>'
                        + '    <td class="center">' + section_name + '</td>'
                        + '    <td class="center">'
                        + '       <div class="action-buttons">'
                        + '            <a title="向上" class="green upbtn" href="javascript:void(0)">'
                        + '                <i class="ace-icon fa fa-arrow-circle-up bigger-130"></i>'
                        + '            </a>'
                        + '            <a title="向下" class="green downbtn" href="javascript:void(0)">'
                        + '                <i class="ace-icon fa fa-arrow-circle-down bigger-130"></i>'
                        + '            </a>'
                        + '            <a title="编辑" class="green" href="/admincourse/section/edit/id/' + section_id + '/" target="_blank">'
                        + '                <i class="ace-icon fa fa-edit bigger-130"></i>'
                        + '            </a>'
                        + '            <a title="删除" class="red deletebtn" href="javascript:void(0)">'
                        + '               <i class="ace-icon fa fa-trash-o bigger-130"></i>'
                        + '            </a>'
                        + '        </div>'
                        + '    </td>'
                        + '    <input name="section_id[]" type="hidden" value="' + section_id + '"/>'
                        + '    <input name="section_name[]" type="hidden" value="' + section_name + '"/>'
                        + '</tr>';
            }

            section_id = null;
            section_name = null;
            $("#section_selected_table").append(proAddHtml);
            $(".upbtn").bind("click", function () {
                var $tr = $(this).parents("tr");
                if ($tr.index() != 0) {
                    $tr.fadeOut().fadeIn();
                    $tr.prev().before($tr);
                }
            });
            $(".downbtn").bind("click", function () {
                var $downbtn = $(".downbtn");
                var len = $downbtn.length;
                var $tr = $(this).parents("tr");
                if ($tr.index() != len - 1) {
                    $tr.fadeOut().fadeIn();
                    $tr.next().after($tr);
                }
            });
            $(".deletebtn").bind("click", function () {
                $(this).parents("tr").remove();
            });
            $("#section").val("");
        } else {
            alert("你还没有选择小节^-^");
        }

    });
    //取消提及的产品
    $(".cancelAdd").click(function () {
        $(this).parent("li").remove();
    });
});
